﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MTManager;
using System.Collections;
using MTControls;

namespace CirqueDuCsDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const bool _simulationEnabled = true;

        private NextImage _nextImage;
        private MTProcessing processing;
        private Queue<ImageWrapper> _images;
        private Random _random;

        public MainWindow()
        {
            InitializeComponent();
            processing = new MTProcessing(MainGrid);
            processing.SimulationEnabled = _simulationEnabled;
            _nextImage = new NextImage();
            _images = new Queue<ImageWrapper>();
            _random = new Random();
        }

        private void click_AddPhoto(object sender, RoutedEventArgs e)
        {
            AddPhoto();
        }

        private void click_RemovePhoto(object sender, RoutedEventArgs e)
        {
            RemovePhoto();
        }

        private void AddPhoto()
        {
            Image image = _nextImage.GetNextImage();
            ImageWrapper wrapper = new ImageWrapper(image);
            Point center = RandomCenter();
            MTCanvas.SetLeft(wrapper, center.X);
            MTCanvas.SetTop(wrapper, center.Y);
            ImageCanvas.Children.Add(wrapper);
            _images.Enqueue(wrapper);
        }

        private void RemovePhoto()
        {
            if (_images.Count == 0) {
                return;
            }
            ImageWrapper leastRecentImage = _images.Dequeue();
            ImageCanvas.Children.Remove(leastRecentImage);
        }

        private Point RandomCenter()
        {
            const double maxDimensionPercentage = 0.75;
            Point center = new Point();
            double maxHeight = ImageCanvas.ActualHeight * maxDimensionPercentage;
            double maxWidth = ImageCanvas.ActualWidth * maxDimensionPercentage;
            center.X = _random.Next((int) maxWidth);
            center.Y = _random.Next((int) maxHeight);
            return center;
        }

    }
}
